import canvasListModule from "@/store/modules/canvas";
import ITextCanvas from "@/interface/canvas/ITextCanvas";
import IText from "@/interface/ITextArea";
import _ from "lodash";
import getTextHeight from "./getTextHeight";

//判断是否点击在了文字区域
function isText(
  cxt: CanvasRenderingContext2D,
  clickX: number,
  clickY: number
): IText {
  const list = _.cloneDeep(canvasListModule.getcanvasList);

  const textList = [];

  for (let i = 0; i < list.length; i++) {
    if (list[i].type == "text") {
      textList.push(list[i] as ITextCanvas);
    }
  }

  for (let i = 0; i < textList.length; i++) {
    const item = textList[i];
    const width = cxt.measureText(item.text).width;
    const height = getTextHeight("14px");
    if (clickX >= item.x - 15 && clickX <= item.x + width + 15) {
      if (
        clickY >= item.y - height / 2 - 4 &&
        clickY <= item.y + height / 2 - 4
      ) {
        return {
          isValue: true,
          text: item.text,
          x: item.x,
          y: item.y,
        };
      }
    }
  }

  return { isValue: false };
}

export default isText;
